Coverage Report

Created: 2024-12-26 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\swift\util.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::compiler::structure::{Field, FixedFieldType};
30
use crate::compiler::util::types::TypeMapper;
31
use crate::compiler::Protocol;
32
use crate::gen::template::util::CaseConversion;
33
use crate::model::protocol::Endianness;
34
use std::borrow::Cow;
35
36
pub struct SwiftTypeMapper<'a> {
37
    proto_name: Cow<'a, str>,
38
}
39
40
impl<'a> SwiftTypeMapper<'a> {
41
0
    pub fn from_protocol(proto: &'a Protocol) -> Self {
42
0
        Self {
43
0
            proto_name: proto.name().to_pascal_case(),
44
0
        }
45
0
    }
46
}
47
48
impl TypeMapper for SwiftTypeMapper<'_> {
49
0
    fn map_local_type<'a>(&self, item_type: &'a str) -> Cow<'a, str> {
50
0
        format!("{}{}", self.proto_name, item_type).into()
51
0
    }
52
53
0
    fn map_foreign_type<'a>(&self, item_type: &'a str) -> Cow<'a, str> {
54
0
        item_type.into()
55
0
    }
56
}
57
58
macro_rules! gen_value_type {
59
    ($prefix: literal, $ty: expr, $suffix: literal) => {
60
        match $ty {
61
            FixedFieldType::Int8 => concat!($prefix, "Int8", $suffix),
62
            FixedFieldType::Int16 => concat!($prefix, "Int16", $suffix),
63
            FixedFieldType::Int32 => concat!($prefix, "Int32", $suffix),
64
            FixedFieldType::Int64 => concat!($prefix, "Int64", $suffix),
65
            FixedFieldType::UInt8 => concat!($prefix, "UInt8", $suffix),
66
            FixedFieldType::UInt16 => concat!($prefix, "UInt16", $suffix),
67
            FixedFieldType::UInt32 => concat!($prefix, "UInt32", $suffix),
68
            FixedFieldType::UInt64 => concat!($prefix, "UInt64", $suffix),
69
            FixedFieldType::Float32 => concat!($prefix, "Float32", $suffix),
70
            FixedFieldType::Float64 => concat!($prefix, "Float64", $suffix),
71
            FixedFieldType::Bool => concat!($prefix, "Bool", $suffix),
72
        }
73
    };
74
}
75
76
pub struct SwiftUtils;
77
78
impl crate::gen::base::structure::Utilities for SwiftUtils {
79
0
    fn get_field_type(field_type: FixedFieldType) -> &'static str {
80
0
        gen_value_type!("", field_type, "")
81
0
    }
82
83
0
    fn get_fragment_name(field: &Field) -> &'static str {
84
0
        let raw_field_type = field.ty.as_fixed().unwrap().bits_type;
85
0
        let raw_field_byte_size = raw_field_type.get_byte_size();
86
0
        match raw_field_byte_size != field.loc.byte_size {
87
0
            true => "unaligned",
88
0
            false => "aligned",
89
        }
90
0
    }
91
92
0
    fn get_bit_codec_inline(endianness: Endianness) -> &'static str {
93
0
        match endianness {
94
0
            Endianness::Little => "BP3DProto.BitCodecLE",
95
0
            Endianness::Big => "BP3DProto.BitCodecBE",
96
        }
97
0
    }
98
99
0
    fn get_byte_codec_inline(endianness: Endianness) -> &'static str {
100
0
        match endianness {
101
0
            Endianness::Little => "BP3DProto.ByteCodecLE",
102
0
            Endianness::Big => "BP3DProto.ByteCodecBE",
103
        }
104
0
    }
105
106
0
    fn get_byte_codec(endianness: Endianness) -> &'static str {
107
0
        match endianness {
108
0
            Endianness::Little => "BP3DProto.ByteCodecLE",
109
0
            Endianness::Big => "BP3DProto.ByteCodecBE",
110
        }
111
0
    }
112
}
113
114
impl crate::gen::base::message::Utilities for SwiftUtils {
115
0
    fn get_value_type(endianness: Endianness, ty: FixedFieldType) -> &'static str {
116
0
        match endianness {
117
0
            Endianness::Little => gen_value_type!("BP3DProto.ValueLE<B, ", ty, ">"),
118
0
            Endianness::Big => gen_value_type!("BP3DProto.ValueBE<B, ", ty, ">"),
119
        }
120
0
    }
121
122
0
    fn get_value_type_inline(endianness: Endianness, ty: FixedFieldType) -> &'static str {
123
0
        match endianness {
124
0
            Endianness::Little => gen_value_type!("BP3DProto.ValueLE<B, ", ty, ">"),
125
0
            Endianness::Big => gen_value_type!("BP3DProto.ValueBE<B, ", ty, ">"),
126
        }
127
0
    }
128
129
0
    fn gen_struct_ref_type(type_name: &str) -> String {
130
0
        format!("{}<B>", type_name)
131
0
    }
132
133
0
    fn gen_message_ref_type(type_name: &str) -> String {
134
0
        format!("{}<B>", type_name)
135
0
    }
136
}